package order;

import model.*;

import javax.swing.JOptionPane;


public class CreateSpy extends UpgradeOrder{
  
	private Territory territory;
	private Spy spy;
	private Player player;
	private TechnologyLevel source;
    private int cost = 35;
       
    public CreateSpy(Territory territory, Player player, TechnologyLevel source){
    	this.territory = territory;
    	this.source = source;
    	this.player = player;
     }
    
    public Territory getSource(){return territory;}
    public Territory getDestination(){return territory;}
    public boolean checkOrder(){  	
    	if (player.getTechnology() < cost){
    		JOptionPane.showMessageDialog(null, territory.getLocation() + "No enough technology to create a spy!");
    		return false;
    	}
    	return true;
    }

    public void undoOrder(){
         player.addTechnology(cost);
         Troop tr = spy.covertBack();
         player.removeSpy(spy);
         territory.addTroop(tr);
   }
    
    public  String printOrder(){
    	StringBuffer sentence = new StringBuffer();
    	sentence.append("At " + territory.getLocation() + ": ");
    	sentence.append("Creat a new spy with a "  + source);
       	sentence.append(" (Cost 35 Technology) ");
    	return sentence.toString();
    }

	@Override
	public void executeOrder() {
		 player.cutTechnology(cost);
    	 Troop tr = territory.peekTroop(source, player);
    	 spy= new Spy(player, tr, territory);
    	 player.addSpy(spy);
    	 territory.removeTroop(source, player);
	}
}
